package com.binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTreeRightSideView0209_199 {
    public List<Integer> rightSideView(TreeNode root) {
        if(root == null) return new ArrayList<>();
        Queue<TreeNode> que = new LinkedList<>();
        List<Integer> res = new ArrayList<>();

        que.offer(root);

        while (!que.isEmpty()){
            int len = que.size();

            while (len > 0){
                TreeNode node = que.poll();

                if(node.left != null) que.offer(node.left);
                if(node.right != null) que.offer(node.right);

                if(len == 1) res.add(node.val);
                len--;
            }
        }
        return res;
    }
}
